/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.button;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.button
 * @date 03/03/2014 - 10:11:24
 */
public class ColorButton extends JButton {
	
	private static final long serialVersionUID = 1157848656638498796L;
	
	private final Component component;
	
	private final String title;
	
	private final Color initialColor;
	
	/**
	 * 
	 */
	public ColorButton() {
	
		this("");
	}
	
	/**
	 * @param initialColor
	 */
	public ColorButton(final Color initialColor) {
	
		this(null, initialColor);
	}
	
	/**
	 * @param component
	 * @param title
	 * @param initialColor
	 */
	public ColorButton(final Component component, final String title, final Color initialColor) {
	
		this.component = component;
		this.title = title;
		this.initialColor = initialColor;
		
		initGUI();
	}
	
	/**
	 * @param title
	 */
	public ColorButton(final String title) {
	
		this(title, Color.WHITE);
	}
	
	/**
	 * @param title
	 * @param initialColor
	 */
	public ColorButton(final String title, final Color initialColor) {
	
		this(null, title, initialColor);
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		setBackground(initialColor);
		setSize(new Dimension(25, 25));
		setPreferredSize(new Dimension(25, 25));
		setMinimumSize(new Dimension(25, 25));
		setMaximumSize(new Dimension(25, 25));
		
		addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(final ActionEvent actionEvent) {
			
				final Color color = JColorChooser.showDialog(component, title, initialColor);
				
				//
				if(color != null) {
					
					setBackground(color);
				}
			}
		});
	}
	
	/**
	 * @return the color
	 */
	public Color getSelectedColor() {
	
		return getBackground();
	}
}
